package com.lenovo.nova.widget;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.ListView;
import com.lenovo.nova.util.Applications;
import com.lenovo.nova.util.debug.MyOldLog;
import com.lenovo.nova.util.debug.mylog;
import com.lenovo.nova.util.debug.slog;

@SuppressLint("NewApi")
public class HelperForAbsListViewOnHover {

    private static final String TAG = "HelperForAbsListViewOnHover";
    AbsListView mInstance;
	
	private Rect mOnHoverRect = new Rect();

	private int onHoverIndex;

	private Drawable mHoverDrawable;
	
	private String simpleName = null;
	private Drawable mTranslucentDrawable;
    private int mHoverState = MotionEvent.ACTION_HOVER_EXIT;

    public HelperForAbsListViewOnHover(AbsListView list) {
		mInstance = list;
		simpleName = list.getClass().getSimpleName();
		mTranslucentDrawable = list.getContext().getResources().getDrawable(android.R.color.transparent);
	}
	
	public void setHoverDrawable(Drawable draw){
		mHoverDrawable = draw;
        mylog.i(TAG,"mHoverDrawable " + mHoverDrawable);
    }

	public void onScrollChanged(int l, int t, int oldl, int oldt) {
		System.out.println(simpleName + " onScrollChanged");
		refreshOnHoverRect(onHoverIndex);
	}
	
	public boolean onHoverEvent(MotionEvent event) {
        mHoverState = event.getAction();

		int x = (int) event.getX();
		int y = (int) event.getY();
        int action = event.getAction();

        mylog.d(TAG,simpleName + " onHoverEvent " + event.getAction() + " x , y " + x + " , " + y );
        if(action == MotionEvent.ACTION_HOVER_EXIT){
            if(!mOnHoverRect.isEmpty()){
                mOnHoverRect.setEmpty();
                onHoverIndex = -1;
                mInstance.invalidate();
            }
        }else {
            int position = mInstance.pointToPosition(x, y);
            mylog.d(TAG," position " + position);
            if(position != -1){
                onHoverIndex = position;
                refreshOnHoverRect(onHoverIndex);
            }else {
                refreshOnHoverRect(-1);
            }
        }

		return false;
	}
	
	public void draw(Canvas canvas){
		mylog.d(TAG,simpleName + " draw " + mHoverDrawable);
		if(mHoverDrawable != null && mHoverState != MotionEvent.ACTION_HOVER_EXIT){
			if(!Applications.get().isInTouchMode()){
				mOnHoverRect.setEmpty();
			}
			mHoverDrawable.setBounds(mOnHoverRect);
			mHoverDrawable.draw(canvas);
            mylog.d(TAG, "mOnHoverRect rect is " + mOnHoverRect);
		}
	}

	public void onInputModeChange(boolean isInTouchMode) {
		mylog.i(TAG,simpleName + " onInputModeChange " + isInTouchMode);
		if (!isInTouchMode) {
            if(mInstance.isFocused()){
                mInstance.setSelector(mHoverDrawable);
                mInstance.setSelection(onHoverIndex);
            }else{
                mOnHoverRect.setEmpty();
                mInstance.invalidate();
            }
		}else{
			mInstance.setSelector(mTranslucentDrawable);
		}
		mInstance.invalidate();
	}

    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
        mylog.i(TAG,simpleName + "onFocusChanged " + gainFocus + "  " + direction + "  " + previouslyFocusedRect + "  isInTouchMode " + isInTouchMode());;

        if(!isInTouchMode()){
            if(gainFocus){
                mInstance.setSelector(mHoverDrawable);
            }else{
                mInstance.setSelector(mTranslucentDrawable);
            }
        }

    }

    private void refreshOnHoverRect(int position) {
        if(position != -1){
            View firView = mInstance.getChildAt(0);
            refreshHoverForListView(firView);

            refreshHoverForGridView(firView);

            mInstance.invalidate(mOnHoverRect);

        }else{
            if(!mOnHoverRect.isEmpty()){
                mOnHoverRect.setEmpty();
                mInstance.invalidate();
            }
        }
    }

    private void refreshHoverForListView(View firView) {
        if(mInstance instanceof ListView){
            ListView temp = (ListView) mInstance;
            if(firView != null){
                firView.getDrawingRect(mOnHoverRect);
                int top = firView.getTop() + (firView.getHeight() + temp.getDividerHeight()) * (onHoverIndex - temp.getFirstVisiblePosition());
                mOnHoverRect.set(mOnHoverRect.left,
                        top,
                        mOnHoverRect.right,
                        top + firView.getHeight());
            }else{
                slog.p("onHoverEvent", "set mOnHoverRect enpty "  + " " + onHoverIndex);
            }
        }
    }

    private void refreshHoverForGridView(View firView) {
        if(mInstance instanceof GridView){
            GridView temp = (GridView) mInstance;
            if(firView != null){
                firView.getDrawingRect(mOnHoverRect);
                int numColumns = temp.getNumColumns();
                int currentPosition = onHoverIndex  - temp.getFirstVisiblePosition();
                if(numColumns != 0){
                    int horizontalSpace = ((GridView) mInstance).getHorizontalSpacing();
                    int verticalSpace = ((GridView) mInstance).getVerticalSpacing();

                    int top = firView.getTop() +
                            (firView.getHeight() + verticalSpace) * ( currentPosition / numColumns) ;

                    int left = firView.getLeft() +
                            (firView.getWidth() + horizontalSpace) * (currentPosition % numColumns);

                    System.out.println(simpleName + " numColumns " + numColumns +
                                    " onHoverIndex " + onHoverIndex +
                                    " temp.getFirstVisiblePosition() " + temp.getFirstVisiblePosition() +
                                    " currentPositoin " + currentPosition +
                                    " top " + top +
                                    " left " + left
                    );
                    mOnHoverRect.set(left,
                            top,
                            left + firView.getWidth(),
                            top + firView.getHeight());

                }else{
                    MyOldLog.e("numColumns is 0");
                }
            }
        }
    }

    private boolean isInTouchMode() {
        return SettingsApplication.get().isInTouchMode();
    }

}
